package com.echoesnet.echoesshortvideo.views;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.lang.reflect.Field;

/**
 * VerticalViewPager
 * Created by kang on 2017/5/4.
 */
public class VerticalViewPager extends ViewPager
{

    public VerticalViewPager(Context context)
    {
        super(context);
        init();
    }

    public VerticalViewPager(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    private void init()
    {
        // The majority of the magic happens here
        setPageTransformer(true, new VerticalPageTransformer());
        // The easiest way to get rid of the overscroll drawing that happens on the left and right
        setOverScrollMode(OVER_SCROLL_NEVER);

        try
        {
            Field field = ViewPager.class.getDeclaredField("mTouchSlop");
            field.setAccessible(true);
            field.setInt(this, 20);
        } catch (NoSuchFieldException e)
        {
            e.printStackTrace();
        } catch (IllegalAccessException e)
        {
            e.printStackTrace();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private class VerticalPageTransformer implements ViewPager.PageTransformer
    {

        @Override
        public void transformPage(View view, float position)
        {

            if (position < -1)
            { // [-Infinity,-1)
                // 当前页的上一页
                view.setAlpha(0);

            }
            else if (position <= 1)
            { // [-1,1]
                view.setAlpha(1);

                // 抵消默认幻灯片过渡
                view.setTranslationX(view.getWidth() * -position);

                //设置从上滑动到Y位置
                float yPosition = position * view.getHeight();
                view.setTranslationY(yPosition);

            }
            else
            { // (1,+Infinity]
                // 当前页的下一页
                view.setAlpha(0);
            }
        }
    }

    /**
     * 交换触摸事件的X和Y坐标
     */
    private MotionEvent swapXY(MotionEvent ev)
    {
        float width = getWidth();
        float height = getHeight();

        float newX = (ev.getY() / height) * width;
        float newY = (ev.getX() / width) * height;

        ev.setLocation(newX, newY);

        return ev;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        if (stopScroll)
        {
            return super.onInterceptTouchEvent(ev);
        }
        else
        {
            boolean intercepted = super.onInterceptTouchEvent(swapXY(ev));
            swapXY(ev);
            return intercepted; //为所有子视图返回触摸的原始坐标
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {
        if (stopScroll)
        {
            return super.onTouchEvent(ev);
        }
        else
        {
            return super.onTouchEvent(swapXY(ev));
        }
    }


    private boolean stopScroll = false;

    /**
     * 设置其是否能滑动换页
     */
    public void setStopScroll(boolean stopScroll)
    {
        this.stopScroll = stopScroll;
    }

}